'''
CodeJam Practice 
Created on 2012-12-20

@author: festony
'''

from cj_lib import *
from properties import *

curr_file_name = 'A-large-practice'
#curr_file_name = 'A-small-practice'
#curr_file_name = 'test'

# map(int, input_lines.pop(0).split(' '))

def input_dividing_func(input_lines):
    total_case = int(input_lines.pop(0))
    case_inputs = []
    for i in range(total_case):
        M, V = map(int, input_lines.pop(0).split(' '))
        b = []
        for j in range((M-1) / 2):
            temp = map(int, input_lines.pop(0).split(' '))
            temp.append(-1)
            b.append(temp)
        for j in range((M+1) / 2):
            b.append([-1, 0, int(input_lines.pop(0))])
        case_inputs.append([M, V, b])
    return case_inputs

def get_parent(index):
    return (index-1)/2

def get_children(index):
    return [index*2+1, index*2+2]

def proc_nodes(G, v0, v1):
    if G == 0:
        return v0 | v1
    if G == 1:
        return v0 & v1
    return -1

def to_desire(b, idx, d):
    if b[idx][2] == d:
        return 0
    # is leaf and not equal to desire: impossible
    if b[idx][0] < 0:
        return 100000000
    ch = get_children(idx)
    r1 = 0
    # OR
    if b[idx][0] == 0:
        if d == 0:
            r1 = to_desire(b, ch[0], 0) + to_desire(b, ch[1], 0)
        #if d == 1:
        else:
            r1 = min(to_desire(b, ch[0], 1), to_desire(b, ch[1], 1))
    # AND
    #if b[idx][0] == 1:
    else:
        if d == 1:
            r1 = to_desire(b, ch[0], 1) + to_desire(b, ch[1], 1)
        #if d == 0:
        else:
            r1 = min(to_desire(b, ch[0], 0), to_desire(b, ch[1], 0))
    if b[idx][1] == 0:
        return r1
    # change gate
    r2 = 0
    # OR
    if b[idx][0] == 1:
        if d == 0:
            r2 = to_desire(b, ch[0], 0) + to_desire(b, ch[1], 0)
        #if d == 1:
        else:
            r2 = min(to_desire(b, ch[0], 1), to_desire(b, ch[1], 1))
    # AND
    #if b[idx][0] == 0:
    else:
        if d == 1:
            r2 = to_desire(b, ch[0], 1) + to_desire(b, ch[1], 1)
        #if d == 0:
        else:
            r2 = min(to_desire(b, ch[0], 0), to_desire(b, ch[1], 0))
    r2 += 1
#    if r1 > r2:
#        print idx, 'changed'
    return min(r1, r2)
    

def process_func(func_input):
    #print func_input
    M, V, b = func_input
    p = len(b) - 1
    while p > 0:
        pp = get_parent(p)
        b[pp][2] = proc_nodes(b[pp][0], b[p][2], b[p-1][2])
        p -= 2
    print b
    r = to_desire(b, 0, V)
    if r >= 100000000:
        r = 'IMPOSSIBLE'
    return r

run_proc(process_func, input_dividing_func, curr_working_folder, curr_file_name)
#print proc_nodes(1, 1, 0)


